import json
from common import errorCode
from flask import jsonify, abort, Response
from yxlog.logger import get_logger

logger = get_logger()


def custom_response(data=None, code=200, msg=None):
    if not msg:
        msg = error_msg(code)
    output = {
        "message": msg,
        "code": code,
        "data": data
    }
    status_code = int(str(code)[0:3])
    if status_code != 200:
        return abort(Response(json.dumps(output), status=status_code))
    return jsonify(output)


def error_msg(code):
    if code == 200:
        return "SUCCESS"
    return errorCode.errorText.get(code, "FAILED")
